package com.taotao.rest.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.taotao.mapper.TbContentMapper;
import com.taotao.pojo.TbContent;
import com.taotao.pojo.TbContentExample;
import com.taotao.pojo.TbContentExample.Criteria;
import com.taotao.rest.dao.JedisClient;
import com.taotao.utils.JsonUtils;

@Component
public class ContentServiceImpl implements ContentService {

	@Autowired
	private TbContentMapper contentMapper;
	@Autowired
	private JedisClient jedisClient;
	@Value("${INDEX_CONTENT_REDIS_KEY}")
	private String INDEX_CONTENT_REDIS_KEY;
	public List<TbContent> getContentList(long categoryId) {
		
		try {
			String result = jedisClient.hget(INDEX_CONTENT_REDIS_KEY, categoryId+"");
			if (result == "" || result == null) {
				throw new Exception("缓存中没有");
			}
			List<TbContent> jsonToList = JsonUtils.jsonToList(result, TbContent.class);
			if (jsonToList == null) {
				throw new Exception("转换失败");
			}
			return jsonToList;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			
			//e.printStackTrace();
		}
		
		
		TbContentExample example = new TbContentExample();
		Criteria criteria = example.createCriteria();
		criteria.andCategoryIdEqualTo(categoryId);
		List<TbContent> list = contentMapper.selectByExample(example);
		try {
			String objectToJson = JsonUtils.objectToJson(list);
			if (objectToJson == "" || objectToJson == null) {
				throw new Exception("object转换json失败");
			}
			jedisClient.hset(INDEX_CONTENT_REDIS_KEY,categoryId+"", objectToJson);
		} catch (Exception e) {
		}
		
		return list;
	}
}
